#include "common.h"

struct pump_operating_paramter operate_parm;

struct timer6_count_variable timer6_count;

struct adaptor_parm_summary adaptor_parm;

void memory_set(void *dst, unsigned char data, unsigned int size)
{
    unsigned char *cursor = (unsigned char *)dst;
    int            i      = 0;
    for (i = 0; i < size; i++) {
        (*cursor) = data;
        cursor++;
    }
}

void memory_copy(void *dst, void *src, unsigned int len)
{
    unsigned char *d = dst;
    unsigned char *s = src;
    for (unsigned int i = 0; i < len; i++) {
        *d = *s;
        d++;
        s++;
    }
}

unsigned int get_running_time(struct time_count *p_time_count)
{
    return (p_time_count->count * TIMER_6_BASE);
}

unsigned int get_time_benchmark(unsigned int time)
{
    return time / TIMER_6_BASE;
}

void set_time_satisfy(struct time_count *p_time_count)
{
    p_time_count->count = p_time_count->benchmark + 1;
}

void set_time_delay(struct time_count *p_time_count, unsigned int delay_ms)
{
    p_time_count->run       = 0;
    p_time_count->count     = 0;
    p_time_count->benchmark = get_time_benchmark(delay_ms);
    p_time_count->run       = 1;
}

unsigned int judge_time_delay(struct time_count *p_time_count)
{
    unsigned char ret = 0;
    if (1 == p_time_count->run) {
        if (p_time_count->benchmark <= p_time_count->count) {
            ret = 1;
        } else {
            ret = 0;
        }
    } else {
        ret = 0;
    }
    return ret;
}

void clear_time_count(struct time_count *p_time_count)
{
    p_time_count->run       = 0;
    p_time_count->count     = 0;
    p_time_count->benchmark = 0;
}

void add_timer_count_variable(struct time_count *p_time_count, unsigned int num)
{
    unsigned char i = 0;
    for (i = 0; i < num; i++) {
        if (1 == p_time_count->run) {
            p_time_count->count++;
        }
        p_time_count++;
    }
}

void delay_time(unsigned int times)
{
    while (times--)
        ;
}

void init_main_var(void)
{
    memory_set(&operate_parm, 0, sizeof(operate_parm));
    memory_set(&timer6_count, 0, sizeof(timer6_count));
}

void init_var(void)
{
    init_main_var();
    init_dma_var();
    get_adaptor_parm();
}

int main(void)
{
    // while(1);
    volatile int sb = sizeof(parm_report_data);
    init_flash();
    init_rcc();
    init_var();
    init_gpio();
    init_can1();
    init_adc1();
    init_dma1();
    init_timer6();
    init_general_timer4();
    init_iwdg();
    set_time_delay(&timer6_count.cycle_10ms, 20);
    while (1) {
        if (judge_time_delay(&timer6_count.cycle_10ms)) {
            set_time_delay(&timer6_count.cycle_10ms, 20);
            feed_iwdg();
            get_sensor_data();
            operate_data_report();
        }

        update_flash_ada_parm();
    }
}
